var color = new Array('#612B2B','grey','#E9B900');
var char = new Array('0','0','x','0','1','1','0','0','f','1','1','0','1','e','0','0','1','1');
var msec = 15000;

function counter(a){a=a/1e3;$("#counter").html(a);setTimeout("decrease()",1e3)}function decrease(){x=$("#counter").html();if(x==0||x==1){$("#counter").html("");return false}$("#counter").html(x-1);setTimeout("decrease()",1e3)}function modif(a,b){function d(){var a=document.documentElement.clientWidth;var b=document.documentElement.clientHeight;var c=$("#name").height();var d=$("#name").width();$("#name").css({top:b/2-c/2-30,left:a/2-d/2})}function c(a,b){return Math.floor(Math.random()*(b-a)+a)}for(i=0;i<30;i++){$("#r"+c(0,35)+"c"+c(0,35)).css({color:b[c(0,b.length)]});$("#r"+c(0,35)+"c"+c(0,35)).html(a[c(0,a.length)])}d();setTimeout("modif(char, color)",200)}var color=new Array("#612B2B","grey","#E9B900");var char=new Array("0","0","x","0","1","1","0","0","f","1","1","0","1","e","0","0","1","1");var msec=15e3

function scan_for_new_messages(){
	$.get("/chat/scan_for_new_messages", {},
		function(data) {
			
		$.each ( $.parseJSON(data), function ( index, element ) { 
			//alert(data);
			var time = element[2].substring(11,element[2].length) ;
			$('#chat_message_list').append("<p>[" + time + "] <span style=''>" + element[0] + "</span>: " + element[1] + "</p>");
			chat_auto_scroll_down();
			$('#no-message').remove();
		} );
		
			

			
	});
	setTimeout("scan_for_new_messages()",500)
}


function add_new_messages(mess, user_id){
	$.post("/chat/add_new_messages", {message : mess, us_id : user_id},
		function(data) {
			$('#no-message').remove();
			addedmess = '<span class="mess_error">Erreur lors de l\'envoi du message</span>';
			if(data == 1){
				addedmess = mess;
				chat_auto_scroll_down();
			}else{
				addedmess = '<span class="mess_error">Erreur lors de l\'enregistrement du message</span>';
			}
			$('#chat_message_list').append("<p>[" + get_current_time() + "] <span style='font-weight: bold;'>" + username + "</span>: " + addedmess + "</p>");
			
	});
	$('#chat-message').val('');
}

function get_current_time(){
	var now 		= new Date();
	var hour        = now.getHours();
	var minute      = now.getMinutes();
	var second      = now.getSeconds();
	return hour + ':' + minute + ':' + second
}

function rezise_chat(){
	$('#chat_message_list').css({'height': $(window).height()-200});
	$('#chat-message').css({'width': $('#chat_message_list').width()-(30*$('#chat_message_list').width()/100)});
	setTimeout("rezise_chat()",1000);
}


function check_who_is_online(){
	$.post("/chat/check_who_is_online", {},
			function(data) {
				users = '';
				
				$.each ( $.parseJSON(data), function ( index, element ) { 
					
					users = users + element;
					if(index != ($.parseJSON(data).length - 1)){
						users = users + ' - ';
					}
				});
				$('#chat_who_is_online').html(users);
		});
	setTimeout("check_who_is_online()",1000);
}


function i_am_entering_chat(user_id){
	$.post("/chat/entering_chat", {us_id : user_id},
			function(data) {
			});
}

function i_am_leaving_chat(user_id){
	$.post("/chat/leaving_chat", {us_id : user_id},
			function(data) {
				
				
		});
}

function chat_auto_scroll_down(){
	//scrollBottom($('#chat_message_list').height());
	var myDiv = $('#chat_message_list');
	myDiv.animate({ scrollTop: myDiv.get(0).scrollHeight }, 100);
}